/**
* Copyright (C) 2018-2020
* All rights reserved, Designed By zhanglin
* 注意：
* 本软件为zhanglin开发研制
 */
package model

import (
	"shop/global"
	"time"
)

type IUserAvatar interface {
	InsertUserAvatar() (int64, error)
	DeleteUserAvatar(id int64) error
	UpdateUserAvatar() error
	FindUserAvatarList() (interface{}, error)
	SplitPageUserAvatarList(pageIndex int64, pageSize int64) (interface{}, error)
}

/**
	UserAvatar实体对象
 **/
type UserAvatar struct {
	Id int64 `json:"id" gorm:"primary_key"` //

	RealName string `json:"real_name"` //真实文件名

	Path string `json:"path"` //路径

	Size string `json:"size"` //大小

	CreateTime time.Time `json:"create_time"` //创建时间
}

func (vo *UserAvatar) InsertUserAvatar() (int64, error) {
	table := global.GDB.Table("user_avatar")
	if err := table.Create(&vo).Error; err != nil {
		return 0, err
	}

	return vo.Id, nil
}

func (vo *UserAvatar) DeleteUserAvatar(id int64) error {
	table := global.GDB.Table("user_avatar")
	table = table.Where("id = ?", id)
	if err := table.Delete(UserAvatar{}).Error; err != nil {
		return err
	}
	return nil
}

func (vo *UserAvatar) UpdateUserAvatar() error {

	table := global.GDB.Table("user_avatar")
	table = table.Where("id = ?", vo.Id)
	if err := table.Updates(map[string]interface{}{
		"id": vo.Id, "real_name": vo.RealName, "path": vo.Path, "size": vo.Size, "create_time": vo.CreateTime}).Error; err != nil {
		return err
	}
	return nil
}

func (vo *UserAvatar) FindUserAvatarList() ([]UserAvatar, error) {
	var lists []UserAvatar
	table := global.GDB.Table("user_avatar")
	if err := table.Find(&lists).Error; err != nil {
		return lists, err
	}
	return lists, nil
}

func (vo *UserAvatar) SplitPageUserAvatarList(pageIndex int64, pageSize int64) (interface{}, error) {
	var lists []UserAvatar
	var total int64
	table := global.GDB.Table("user_avatar")
	table = table.Select("  id,   real_name,   path,   size,   create_time   ")
	if err := table.Count(&total).Error; err != nil {
		return lists, err
	}
	table = table.Offset((pageIndex - 1) * pageSize).Limit(pageSize)
	if err := table.Find(&lists).Error; err != nil {
		return lists, err
	}

	m := make(map[string]interface{})
	m["lists"] = lists
	m["total"] = total
	return m, nil
}
